package com.slfx.data.permission.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.slfx.data.permission.dto.CreateUserDataAuthReqDTO;
import com.slfx.data.permission.dto.QueryUserDataAuthPageReqDTO;
import com.slfx.data.permission.entity.DataAuth;
import com.slfx.data.permission.entity.UserDataAuth;
import com.slfx.data.permission.service.UserDataAuthService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author: slfx
 * @Date: 2023-01-02
 * @Description:
 **/
@RestController
@RequestMapping("/userDataAuth")
public class UserDataAuthController {

    @Resource
    private UserDataAuthService userDataAuthService;

    /**
     * 根据ID获取
     *
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public UserDataAuth getById(@RequestParam("id") Long id) {
        UserDataAuth userDataAuth = userDataAuthService.getById(id);
        return userDataAuth;
    }

    /**
     * 新增用户的权限
     *
     * @param reqDTO
     * @return
     */
    @PostMapping("/save")
    public boolean save(@RequestBody @Valid CreateUserDataAuthReqDTO reqDTO) {
        //删除原来的数据权限
        userDataAuthService.remove(new LambdaQueryWrapper<UserDataAuth>()
                .eq(UserDataAuth::getDataCode, reqDTO.getDataCode())
                .eq(UserDataAuth::getUserCode, reqDTO.getUserCode()));
        List<UserDataAuth> dataAuthList = Lists.newArrayList();
        for (CreateUserDataAuthReqDTO.Item item : reqDTO.getItemList()) {
            UserDataAuth dataAuth = new UserDataAuth();
            dataAuth.setAuthCode(item.getAuthCode());
            dataAuth.setAuthName(item.getAuthName());
            dataAuth.setUserCode(reqDTO.getUserCode());
            dataAuth.setDataCode(reqDTO.getDataCode());
            dataAuthList.add(dataAuth);
        }
        //新增用户权限
        boolean b = userDataAuthService.saveBatch(dataAuthList);
        return b;
    }

    /**
     * 获取用户的权限列表
     *
     * @param userCode
     * @param dataCode
     * @return
     */
    @GetMapping("/queryUserDataAuthList")
    public List<UserDataAuth> queryUserDataAuthList(@RequestParam("userCode") String userCode, @RequestParam("dataCode") String dataCode) {
        List<UserDataAuth> list = userDataAuthService.list(new LambdaQueryWrapper<UserDataAuth>()
                .eq(UserDataAuth::getDataCode, dataCode)
                .eq(UserDataAuth::getUserCode, userCode));
        return list;
    }

    /**
     * 获取用户的权限分页
     *
     * @param reqDTO
     * @return
     */
    @PostMapping("/queryUserDataAuthPage")
    public IPage<UserDataAuth> queryUserDataAuthPage(@RequestBody QueryUserDataAuthPageReqDTO reqDTO) {
        //分页参数
        Page<UserDataAuth> page = new Page<>(reqDTO.getPageNo(), reqDTO.getPageSize());
        IPage<UserDataAuth> authIPage = userDataAuthService.page(page, new LambdaQueryWrapper<UserDataAuth>()
                .eq(UserDataAuth::getDataCode, reqDTO.getDataCode())
                .eq(UserDataAuth::getUserCode, reqDTO.getUserCode()));
        return authIPage;
    }


}
